﻿using System;
using System.Diagnostics.Contracts;
using Isuka.Automation;

namespace Isuka.Excel
{
    partial class XExtensions
    {
        public static XRange To(this XRange begin, XRange end)
        {
            Contract.Requires<ArgumentNullException>(begin != null, "begin");
            Contract.Requires<ArgumentNullException>(end != null, "end");
            Contract.Ensures(Contract.Result<XRange>() != null);

            return begin.Worksheet.Range(begin, end);
        }

        public static XRange ToEnd(this XRange begin, XDirection dir)
        {
            Contract.Requires<ArgumentNullException>(begin != null, "cell");
            Contract.Ensures(Contract.Result<XRange>() != null);

            object result = AutomationUtility.GetObject(begin).End[(int)dir];
            Contract.Assume(result != null);
            using (var end = new XRange(begin.Worksheet, result))
            {
                return begin.To(end);
            }
        }
    }
}
